package com.ewanghuiju.app.ui.pintuan.activity;

import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.base.BaseFragment;
import com.ewanghuiju.app.base.contract.pintuan.PintuanClassifyContract;
import com.ewanghuiju.app.component.ImageLoader;
import com.ewanghuiju.app.model.bean.response.PintuanClassifyResponBean;
import com.ewanghuiju.app.model.http.request.BaseRequest;
import com.ewanghuiju.app.presenter.pintuan.PintuanClassifyPresenter;
import com.ewanghuiju.app.test.bean.PintuanClassifyRightBean;
import com.ewanghuiju.app.ui.home.activity.SearchShopActivity;
import com.ewanghuiju.app.ui.pintuan.adapter.ClassifyOfPintuanNameAdapter;
import com.ewanghuiju.app.ui.pintuan.adapter.PintuanClassifyRightAdapter;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class describe
 * @time 2020/2/18
 */
public class PintuanClassifyFragment extends BaseFragment<PintuanClassifyPresenter> implements PintuanClassifyContract.View {

    @BindView(R.id.classic_recycle)
    RecyclerView classOfPintuanNameView;
    @BindView(R.id.classic_right_view)
    RecyclerView classicRightView;
    @BindView(R.id.fragment_toolbar)
    LinearLayout fragmentToolbar;
    @BindView(R.id.frame)
    LinearLayout frame;

    /**
     * 上一个被点击的item
     */
    private int recycleViewCanShowHeight;
    private int lastPosition;

    private ClassifyOfPintuanNameAdapter adapter;
    private List<PintuanClassifyResponBean> list;

//    private List<PintuanClassifyRightBean> rightList;


    private PintuanClassifyRightAdapter rightAdapter;


    public static PintuanClassifyFragment newInstance() {
        Bundle bundle = new Bundle();
        PintuanClassifyFragment fragment = new PintuanClassifyFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

//    @Override
//    protected int getLayout() {
//        return R.layout.activity_pintuan_classify;
//    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pintuan_classify;
    }

    @Override
    protected void initEventAndData() {
        LoadingDialogUtils.show(activity);
        mPresenter.getPticatesAll();
    }

    private void setRightData(PintuanClassifyRightBean bean) {
        if (rightAdapter == null) {
            rightAdapter = new PintuanClassifyRightAdapter(R.layout.item_pintuan_classify_right);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
            classicRightView.setLayoutManager(linearLayoutManager);
            classicRightView.setAdapter(rightAdapter);
        }
        if (!TextUtils.isEmpty(bean.getBanner())) {
            View view = getLayoutInflater().inflate(R.layout.item_pintuan_ejclassify_head, classicRightView, false);
            ImageView riv = view.findViewById(R.id.rv_head);
            ImageLoader.load(context, bean.getBanner(), riv);
            rightAdapter.addHeaderView(view);
        }
        if (bean.getTwo() != null && bean.getTwo().size() > 0) {
            rightAdapter.setNewData(bean.getTwo());
        }
    }

    @Override
    public void showContent(String list) {

    }

    @Override
    public void showMoreContent(String list) {

    }

    @Override
    public void showPticatesAll(List<PintuanClassifyResponBean> pcrList) {
        if (pcrList != null && pcrList.size() > 0) {
            setLeftData(pcrList);
            BaseRequest baseRequest = new BaseRequest();
            baseRequest.setId(pcrList.get(0).getId() + "");
            mPresenter.getPticatesToo(baseRequest);
        }
    }

    @Override
    public void showPticatesToo(PintuanClassifyRightBean bean) {
        LoadingDialogUtils.dismissDialog_ios();
        setRightData(bean);
    }

    /**
     * 获取左边数据
     */
    private void setLeftData(List<PintuanClassifyResponBean> list) {
        list.get(lastPosition).setIsSelect(1);
        classOfPintuanNameView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        adapter = new ClassifyOfPintuanNameAdapter(R.layout.classify_of_pintuan_name_view_item);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (lastPosition == position) return;
                if (recycleViewCanShowHeight == 0) {
                    recycleViewCanShowHeight = classOfPintuanNameView.getHeight();
                }
                list.get(lastPosition).setIsSelect(0);
                list.get(position).setIsSelect(1);
                if (adapter != null) {
                    adapter.notifyDataSetChanged();
                }
                LoadingDialogUtils.show(activity);
                rightAdapter.removeAllHeaderView();
                rightAdapter.setNewData(new ArrayList<>());
                PintuanClassifyResponBean pintuanClassifyResponBean = (PintuanClassifyResponBean) adapter.getItem(position);
                BaseRequest baseRequest = new BaseRequest();
                baseRequest.setId(pintuanClassifyResponBean.getId() + "");
                mPresenter.getPticatesToo(baseRequest);

                if (recycleViewCanShowHeight > 0 && Build.VERSION.SDK_INT > 10) {
                    classOfPintuanNameView.smoothScrollBy(0, (int) (view.getY() - recycleViewCanShowHeight / 2 + view.getPivotY()));
                }
                lastPosition = position;
            }
        });
        classOfPintuanNameView.setAdapter(adapter);
        adapter.setNewData(list);
//        adapter.notifyDataSetChanged();
    }

    @OnClick({R.id.go_search})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.go_search:
                new StartActivityUtil(context, SearchShopActivity.class).startActivity(true);
                break;
            default:
                break;
        }
    }

    @Override
    public void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).titleBar(fragmentToolbar)
                .init();
    }
}
